import {post} from '@/common/request';

export const videoMd5Key = 'BJUPIXNGYL';
//调试开发 - 门户
// export const urlContent = "http://192.168.1.234:19190/"; // 马晓杰
// export const urlContent = "http://192.168.2.203:19200/"; // 凌国辉
// export const urlContent = "http://192.168.2.252:19159/"; // 天悦
// export const urlContent = "http://10.144.221.108:19019/"; // 天悦
// export const urlContent = "http://192.168.2.62:19019/"; // 晓杰
// export const urlContent = "http://114.242.25.77:19019/"; // 晓杰 公网
// export const urlContent = "http://10.144.221.108:19019/"; // 晓杰 内网穿透
// export const urlContent = "http://10.144.221.176:19159/"; // 刘赟 内网穿透
// export const urlContent = "http://192.168.2.140:19159/"; // 冯辉
// export const urlContent = "http://192.168.2.140:19019/"; // 冯辉
// export const urlContent = "http://192.168.11.23:19158/"; // 新鸾
// export const urlContent = "http://192.168.11.24:19159/"; // 天悦; // 新鸾
// export const urlContent = "http://192.168.11.29:19018/"; // 张硕
//  export const urlContent = "http://192.168.11.26:19019/"; // 杰哥
// export const urlContent = "http://192.168.11.24:19019/"; 
// export const urlContent = "http://192.168.11.33:19019/"; // 佳欣
// 打包服务配置
let urlContent = "https://practice-qa.bjjrzy.com:1000/"; // 测试
// let urlContent = "https://webtest.bjupi.com:8017/"; // 测试
// let urlContent = "https://practice-qa.bjjrzy.com:1000/"; // 测试
// let urlContent = "https://gyl.bjupi.com/"; // 正式

export const urlSocketContent = urlContent + "api-message/";
// 开网关
export const urlApiClientContent = urlContent + 'api-client/';
// 关网关
// export const urlApiClientContent = urlContent;
export const urlApiManage = urlContent + 'api-manage/';
export const urlPublicContent = urlApiClientContent + 'public/';

export const urlPublicSprogramContent = urlPublicContent + 'sprogram/'



/**
 * 获取验证令牌
 * @returns {string|string}
 */
const getAppKey = () => {
    let loginDataKey = uni.getStorageSync('appKey');
    return loginDataKey ? `appKey=${loginDataKey}` : '';
};

/**
 * 清除登录信息
 * @returns {string|string}
 */
export const removeAppKeyMenus = () => {
    uni.clearStorage();
};


/**
 * 请求参数拼接
 * @param thisUrl
 * @param data
 * @returns {string}
 */
export function requestParams(thisUrl, data) {
    const getAppKeyData = getAppKey();
    const params = Object.entries(data).map(([key, value]) => `${key}=${value}`);
    const timestampString = `?${Date.now()}`;
	console.log(params)
    const queryString = params.length ? `&${params.join('&')}` : '';
    const appKeyString = getAppKeyData ? `&${getAppKeyData}` : '';

    return `${thisUrl}${timestampString}${appKeyString}${queryString}`;
}

class URLSearchParamsPolyfill {
    constructor(init = '') {
        this.params = {};

        if (typeof init === 'string') {
            this._parseFromString(init);
        } else if (typeof init === 'object') {
            for (let key in init) {
                if (init.hasOwnProperty(key)) {
                    this.append(key, init[key]);
                }
            }
        }
    }

    _parseFromString(queryString) {
        queryString = queryString.startsWith('?') ? queryString.substring(1) : queryString;
        const pairs = queryString.split('&');
        pairs.forEach(pair => {
            const [key, value] = pair.split('=');
            this.append(decodeURIComponent(key), decodeURIComponent(value || ''));
        });
    }

    append(name, value) {
        if (!this.params[name]) {
            this.params[name] = [];
        }
        this.params[name].push(String(value));
    }

    delete(name) {
        delete this.params[name];
    }

    get(name) {
        return this.params[name] ? this.params[name][0] : null;
    }

    getAll(name) {
        return this.params[name] || [];
    }

    has(name) {
        return this.params.hasOwnProperty(name);
    }

    set(name, value) {
        this.params[name] = [String(value)];
    }

    forEach(callback) {
        for (let key in this.params) {
            if (this.params.hasOwnProperty(key)) {
                this.params[key].forEach(value => {
                    callback(value, key, this);
                });
            }
        }
    }

    toString() {
        const pairs = [];
        this.forEach((value, key) => {
            pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
        });
        return pairs.join('&');
    }
}

/**
 * 新版处理方式
 */
export function requestNewParam(paramObj) {
    const timestamp = Date.now();
    const getAppKeyData = getAppKey();
    const appKeyStr=getAppKeyData?`&${getAppKeyData}`:"";
    let paramData = "";
    if (typeof paramObj === 'object' && paramObj !== null && Object.keys(paramObj).length > 0) {
        const params = new URLSearchParamsPolyfill();
        for (const key in paramObj) {
            // 排除成功回调
            if(key!=="success"){
                params.append(key, paramObj[key]);
            }
        }
        paramData = `&${params.toString()}`
    }
    return `?${timestamp}${appKeyStr}${paramData}`;
}

// /**
//  * 登录
//  * @param phone
//  * @param verificationCode
//  * @returns {Promise<*>}
//  */
// export function login(phone, verificationCode) {
//     return post(urlPublicSprogramContent + requestParams('login', {phone, verificationCode}), null, false);
// }

/**
 * 登录接口
 * @param loginName
 * @param password
 * @returns {Promise<{data: Response} | void>}
 */
export function miniLogin(loginName, password,) {
    return post(urlPublicContent + requestParams('login',{ loginName, password}), null);
}

/**
 * 验证码登录
 * @param phone
 * @param verifyCode
 * @returns {Promise<{data: Response} | void>}
 */
export function mobileLogin(phone, verifyCode) {
    return post(urlPublicContent + requestParams('mobileLogin',{phone,verifyCode}), null);
}


/**
 * 获取DictCode
 * @param keyStrs key
 * @return
 */
export function mapDictCodes(keyStrs) {
    return post(urlPublicContent + requestParams('dictCode/mapClientDictCodes', {keyStrs}), null);
}

export function onDict(keyStrs) {
    return post(urlApiManage + requestParams('mapDictCodes', {keyStrs}), null);
}

/**
 * 发送验证码
 * @param phone
 * @param type 4 登录验证码 3修改手机号 2修改密码
 * @returns {Promise<{data: Response} | void>}
 */
export function sendCode(phone, type) {
    return post(urlPublicContent + requestParams('sendCode',{phone, type}), null);
}